package com.jason.kotlin.meta.architecture2.ui.fragments

import android.os.Build
import android.view.View
import androidx.annotation.RequiresApi
import androidx.appcompat.widget.Toolbar
import androidx.viewpager.widget.ViewPager
import com.google.android.material.tabs.TabLayout
import com.jason.kotlin.meta.R
import com.jason.kotlin.meta.architecture2.base.WanBaseFragment
import com.jason.kotlin.meta.architecture2.ui.LoginActivity
import com.jason.library.common.adapter.CommonViewPagerAdapter
import com.jason.library.common.ext.start

class TreeFragment : WanBaseFragment() {

    private lateinit var toolbar: Toolbar
    private lateinit var viewPager: ViewPager
    private lateinit var tabLayout: TabLayout

    private lateinit var pagerAdapter: CommonViewPagerAdapter

    override fun getLayoutId() = R.layout.fragment_tree

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun onViewInit(view: View) {
        toolbar = view.findViewById(R.id.toolbar)

        toolbar.apply {
            inflateMenu(R.menu.todo_menu)
            setOnMenuItemClickListener {
                when (it.itemId) {
                    R.id.todo_add -> start(LoginActivity::class.java)
                }
                true
            }
        }
        tabLayout = view.findViewById(R.id.tablayout)
        viewPager = view.findViewById(R.id.viewpager)

        pagerAdapter = CommonViewPagerAdapter(
            childFragmentManager
        )
        pagerAdapter.addFragment(SquareFragment(), getString(R.string.wan_square_title))
        pagerAdapter.addFragment(SystemFragment(), getString(R.string.wan_system_title))
        pagerAdapter.addFragment(NavigationFragment(), getString(R.string.wan_navigation_title))

        viewPager.adapter = pagerAdapter
        viewPager.offscreenPageLimit = pagerAdapter.count
        tabLayout.setupWithViewPager(viewPager)
    }

    override fun onDataInit() {

    }


}